<?php
/*
*  -------------------------------------------------
*   @file		: .php
*   @link		:CPC  www.chinapet.com
*   @copyright	: 2002-2012 Xingmo Inc
*   @author		: Laipiyang <462166282@qq.com>
*   @blog_url   : http://www.icuir.com/
*   @blog_url   : http://blog.sina.com.cn/xmphp
*   @date		:
*   @update		:
*  -------------------------------------------------
*/
include_once './include/init.inc.php';
$filename=basename(__FILE__);
//checklogin();
$act=isset ( $_GET['act'] ) ? trim ( $_GET['act'] ) : '' ;
if($act=='add')
{
	$smarty->assign('a',array('title'=>'添加道具','act'=>'insert','btn'=>'道具添加完成'));
	$smarty->assign('sort_list',$db->fetch_all("SELECT * FROM `info_q_usual_sort` WHERE `type`='3'"));
	$smarty->display(ADMIN_TPL.'scene_info.dwt');

}
else if ($act=='insert')
{
	   $scene_name=isset ( $_POST['scene_name'] ) ? trim ( $_POST['scene_name'] ) : '' ;
	   $sort_id=isset ( $_POST['sort_id'] ) ? intval ( $_POST['sort_id'] ) : 0;
	   $link_url=isset ( $_POST['link_url'] ) ? trim ( $_POST['link_url'] ) : '' ;
	   $sales=isset ( $_POST['sales'] ) ? intval ( $_POST['sales'] ) : 0;
	   $order=(isset($_POST['order']) && $_POST['order'] <>'' ) ? intval($_POST['order']) : 30;
	   $enable=isset ( $_POST['enable'] ) ? intval ( $_POST['enable'] ) : 0 ;
	   $attribute=isset ( $_POST['attribute'] ) ? trim ( $_POST['attribute'] ) : '' ;
	   $photo=isset($_FILES['photo']) ? $_FILES['photo'] : array();
	   $dateline=time();

	 if($scene_name=='' || empty($scene_name))
	 {
		 alert('道具名称不可以为空');
		 exit();
	 }
	 if(strlen($scene_name)<4 || strlen($scene_name)>40)
	 {
		 alert('道具名称少于两个字符!');
		 exit;
	 }
	 if(empty($sales))
	 {
		 alert('道具售价不可以为空...');
		 exit;
	 }
	 if($db->fetch_one("SELECT * FROM `info_q_scene` WHERE `scene_name`='{$scene_name}'"))
	 {
		 alert('该道具已经存在,请重新选择');
		 exit;
	 }
	 if(empty($sort_id) || $sort_id==0)
	 {
		alert('对不起,道具分类必选');
		exit;
	 }

	if($link_url)
	{
		  if(strlen($link_url) < 6 || strlen($link_url) > 150)
		  {
			  alert('链接地址非法');
			  exit;
		  }
	}
    if(!empty($photo['name']))
    {
		if($photo['error']=='0')
		{
			if(is_uploaded_file($photo['tmp_name']))
			{
				$upload=new UploadFile();
				$file_dir=ROOT.'/upload/scene/';
				$imgs_name=$upload->upload($file_dir,$photo,2000000);  //默认每张图片控制在2M之内
			}
		}

    }
	else
	{
		alert('必须传一张道具图');
		exit;
	}
	//sql insert
	$sql="INSERT INTO `info_q_scene` (`scene_name`,`sort_id`,`link_url`,`sales`,`order`,`enable`,`attribute`,`photo`,`dateline`) VALUES ('{$scene_name}','{$sort_id}','{$link_url}','{$sales}','{$order}','{$enable}','{$attribute}','{$imgs_name}','{$dateline}') ";
	if($db->query($sql))
	{
		alert('道具添加成功',$filename);
		exit;
	}
	else
	{
		alert('道具添加失败');
		exit;
	}
}
else if ($act=='edit')
{
	  $scene_id=isset ( $_GET['scene_id'] ) ? intval ( $_GET['scene_id'] ) : 0 ;
	  if($scene_id<1)
	  {
		 alert('操作非法');
		 exit;
	  }
	  $sql="SELECT * FROM `info_q_scene` WHERE `scene_id`='{$scene_id}' ";
	  $query=$db->fetch_one($sql);
	  if(!$query)
	  {
		alert('对不起,记录不存在');
		exit;
	  }
	  echo '<pre>';
	  print_r ($query);
	  echo '</pre>';
	 $smarty->assign('r',$query);
	 $smarty->assign('sort_list',$db->fetch_all("SELECT * FROM `info_q_usual_sort` WHERE `type`='3'"));
	 $smarty->assign('a',array('title'=>'修改道具页面','act'=>'update','btn'=>'修改道具完成'));
	 $smarty->display(ADMIN_TPL.'scene_info.dwt');

}
else if ($act=='update')
{
	   $scene_id=isset ( $_POST['scene_id'] ) ? intval ( $_POST['scene_id'] ) : 0;
	   if($scene_id<1)
	   {
		  alert('操作非法');
		  exit;
	   }
	   $scene_licuicui=isset ( $_POST['scene_licuicui'] ) ? trim ( $_POST['scene_licuicui'] ) : '' ;
	   $scene_name=isset ( $_POST['scene_name'] ) ? trim ( $_POST['scene_name'] ) : '' ;
	   $sort_id=isset ( $_POST['sort_id'] ) ? intval ( $_POST['sort_id'] ) : 0;
	   $link_url=isset ( $_POST['link_url'] ) ? trim ( $_POST['link_url'] ) : '';
	   $sales=isset ( $_POST['sales'] ) ? intval ( $_POST['sales'] ) : 0;
	   $order=(isset($_POST['order']) && $_POST['order'] <>'' ) ? intval($_POST['order']) : 30;
	   $enable=isset ( $_POST['enable'] ) ? intval ( $_POST['enable'] ) : 0 ;
	   $attribute=isset ( $_POST['attribute'] ) ? trim ( $_POST['attribute'] ) : '' ;
	   $photo=isset($_FILES['photo']) ? $_FILES['photo'] : array();
	   $dateline=time();
		 if($scene_name=='' || empty($scene_name))
		 {
			 alert('道具名称不可以为空');
			 exit();
		 }
		 if(strlen($scene_name)<4 || strlen($scene_name)>40)
		 {
			 alert('道具名称少于两个字符!');
			 exit;
		 }
		 if($db->fetch_one("SELECT * FROM `info_q_scene` WHERE `scene_name`='{$scene_name}' AND `scene_id`<>'{$scene_id}'"))
		 {
			 alert('该道具已经存在,请重新选择');
			 exit;
		 }
		 if(empty($sort_id) || $sort_id==0)
		 {
			alert('对不起,道具分类必选');
			exit;
		 }

		if($link_url)
		{
			  if(strlen($link_url) < 6 || strlen($link_url) > 150)
			  {
				  alert('链接地址非法');
				  exit;
			  }
		}

		if(empty($sales) || $sales=='0')
		{
			alert('道具售价必填!');
			exit;
		}

		if(empty($photo['name']))
		{
			$imgs=$scene_licuicui;
		}
		else
		{
			if(is_uploaded_file($photo['tmp_name']))
			{
					$upload=new UploadFile();
					$file_dir = ROOT.'upload/scene/';  //设置上传目录
					$imgs=$upload->upload($file_dir,$photo,1000000);
//					$source_path=$file_dir.substr($scene_licuicui,0,2);
//					echo  $source_path;
//					if(@is_dir($source_path))
//					{
//						 delDirAndFile($source_path);
//					}
			}
		}
		//sql update
		$sql="UPDATE `info_q_scene` SET `scene_name`='{$scene_name}',`sort_id`='{$sort_id}',`link_url`='{$link_url}',`sales`='{$sales}',`order`='{$order}',`enable`='{$enable}',`attribute`='{$attribute}',`photo`='{$imgs}',`dateline`='{$dateline}'  WHERE `scene_id`='{$scene_id}'";
		if($db->query($sql))
		{
			alert('道具修改成功...',$filename);
			exit;
		}
		else
		{
			alert('道具修改成功');
			exit;
		}

}
else if ($act=='del')
{
   $scene_id=isset ( $_GET['scene_id'] ) ? intval ( $_GET['scene_id'] ) : '' ;
   if(!$scene_id)
   {
	 alert('操作非法');
	 exit;
   }
   $sql="SELECT * FROM `info_q_scene` WHERE `scene_id`='{$scene_id}'";
   $query=$db->fetch_one($sql);
   if(!$query)
   {
	 alert('数据不存在 ,请重新输入');
	 exit;
   }
//   $file_dir=ROOT.'upload/scene/'.substr($query['photo'],0,2);  //获取删除的目录
//   if(is_dir($file_dir))
//   {
//      delDirAndFile($file_dir);
//   }
   $sql="DELETE FROM `info_q_scene` WHERE `scene_id`='{$scene_id}'";
   $res=$db->query($sql);
   if($res)
   {
	   alert('删除成功',$filename);
	   exit;
   }
   else
   {
	   alert('删除失败');
	   exit;
   }
}
else if ($act=='change_state')
{
     $scene_id=isset ( $_POST['scene_id'] ) ? intval ( $_POST['scene_id'] ) : 0;
	 if($scene_id<1)
	 {
		alert('操作非法');
		exit();
	 }
	 $sql="SELECT * FROM `info_q_scene` WHERE `scene_id`='{$scene_id}' ";
	 $query =$db->fetch_one($sql);
	 if(!$query)
	 {
		 echo 2;
		 exit;
	 }
	 echo $a=$query['enable'] ? 0 : 1 ;
	 $sql="UPDATE `info_q_scene` SET `enable`='{$a}' WHERE `scene_id`='{$scene_id}' ";
	 $db->query($sql);
}
else
{
	$smarty->assign('pl',$pl=licuicui_page($db->num_rows($db->query("SELECT * FROM `info_q_scene`")),$filename,9));
	$smarty->assign('scene_list',$db->fetch_all("SELECT q.*,s.`sort_name`  FROM `info_q_scene` AS q LEFT JOIN `info_q_usual_sort` AS s ON q.`sort_id`=s.`sort_id`  LIMIT {$pl['start']},{$pl['pagesize']}"));
	$smarty->assign('web',array('title'=>'道具列表'));
	$smarty->display(ADMIN_TPL.'scene_list.dwt');

}

?>